﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using DocToolkit;
using DrawTools;

namespace TestMap
{
    public enum DrawToolType
    {
        Pointer,
        Rectangle,
        Ellipse,
        Line,
        Polygon,
        NumberOfDrawTools
    };

    public partial class Form1 : Form
    {
        private GraphicsList graphicsList;    // list of draw objects
        private DrawToolType activeTool;      // active drawing tool
        private Tool[] tools;                 // array of tools

        private DocManager docManager;

        private ContextMenuStrip m_ContextMenu;

        public Form1()
        {
            InitializeComponent();
            
          
            InitMap();
            ToolStripSetup(mapControl);
            InitDrawTool();
        }

        public void ToolStripSetup(AxMapXLib.AxMap AxMap1)
        {
            mapControl = AxMap1;
            //// Create the ImageList
            ImageList imgList = new ImageList();
            imgList.ImageSize = new Size(18, 16);
            imgList.ColorDepth = ColorDepth.Depth4Bit;
            imgList.TransparentColor = Color.FromArgb(192, 192, 192);

            // adds the bitmap
            if (File.Exists("buttons.bmp"))
            {
                //path = System.IO.Path.GetFullPath("buttons.bmp");
                //bmp = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream(this.GetType(), "buttons.bmp"));
                Bitmap bmp = new Bitmap("buttons.bmp");
                imgList.Images.AddStrip(bmp);
            }

            toolStripButton1.Image = imgList.Images[15];
            toolStripButton2.Image = imgList.Images[16];
            toolStripButton3.Image = imgList.Images[18];
            toolStripButton4.Image = imgList.Images[9];
        }

        public void InitMap()
        {
            mapControl.GeoSet = Application.StartupPath + "\\bando.gst";
            mapControl.Title.Visible = false;
            mapControl.Zoom = 100000;

            mapControl.CurrentTool = MapXLib.ToolConstants.miZoomOutTool;
            //MapXLib.CoordSys mydcs = mapControl.DisplayCoordSys;
            //mapControl.NumericCoordSys = mydcs;
            //mapControl.InfotipSupport = false;
            //mapControl.PaperUnit = MapXLib.PaperUnitConstants.miPaperUnitPoint;
            mapControl.Layers.AddUserDrawLayer("Symbols", 1);

            //double zoom = mapControl.Zoom;
            //double cX = mapControl.CenterX;
            //double cY = mapControl.CenterY;
            //mapControl.CreateCustomTool(3, MapXLib.ToolTypeConstants.miToolTypePoint,
            //                            MapXLib.CursorConstants.miArrowQuestionCursor);
        }

        public void InitDrawTool()
        {
            // create list of graphic objects
            graphicsList = new GraphicsList();

            //tools = new Tool[1];
            //tools[0] = new ToolRectangle();
            // create array of drawing tools
            tools = new Tool[(int)DrawToolType.NumberOfDrawTools];
            tools[(int)DrawToolType.Pointer] = new ToolPointer();
            tools[(int)DrawToolType.Rectangle] = new ToolRectangle();
            tools[(int)DrawToolType.Ellipse] = new ToolEllipse();
            tools[(int)DrawToolType.Line] = new ToolLine();
            tools[(int)DrawToolType.Polygon] = new ToolPolygon();
        }

        /// <summary>
        /// Active drawing tool.
        /// </summary>
        public DrawToolType ActiveTool
        {
            get
            {
                return activeTool;
            }
            set
            {
                activeTool = value;
            }
        }

        /// <summary>
        /// List of graphics objects.
        /// </summary>
        public GraphicsList GraphicsList
        {
            get
            {
                return graphicsList;
            }
            set
            {
                graphicsList = value;
            }
        }

        public AxMapXLib.AxMap MapControl
        {
            get
            {
                return mapControl;
            }
        }

        /// <summary>
        /// Reference to DocManager
        /// </summary>
        public DocManager DocManager
        {
            get
            {
                return docManager;
            }
            set
            {
                docManager = value;
            }
        }

        private void btnRect_Click(object sender, EventArgs e)
        {
            Graphics dc = mapControl.CreateGraphics();
            Pen pen = new Pen(Color.Red);
            dc.DrawRectangle(pen, 100, 100, 200, 300);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            mapControl.CurrentTool = MapXLib.ToolConstants.miZoomInTool;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            mapControl.CurrentTool = MapXLib.ToolConstants.miZoomOutTool;
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            mapControl.CurrentTool = MapXLib.ToolConstants.miPanTool;
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            mapControl.CurrentTool = MapXLib.ToolConstants.miSelectTool;
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            mapControl.CurrentTool = MapXLib.ToolConstants.miArrowTool;
            activeTool = DrawToolType.Rectangle;
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            mapControl.CurrentTool = MapXLib.ToolConstants.miArrowTool;
            activeTool = DrawToolType.Ellipse;
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            mapControl.CurrentTool = MapXLib.ToolConstants.miArrowTool;
            activeTool = DrawToolType.Line;
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            mapControl.CurrentTool = MapXLib.ToolConstants.miArrowTool;
            activeTool = DrawToolType.Pointer;
        }

        private void mapControl_MouseDownEvent(object sender, AxMapXLib.CMapXEvents_MouseDownEvent e)
        {
            if (e.button == 1)
                tools[(int)activeTool].OnMouseDown(this, e);
            //else if (e.button == 2)
            //    OnContextMenu(e);
        }

        private void mapControl_DrawUserLayer(object sender, AxMapXLib.CMapXEvents_DrawUserLayerEvent e)
        {
            Graphics myGraphics = Graphics.FromHdc(new IntPtr(e.hOutputDC));
            SolidBrush brush = new SolidBrush(Color.FromArgb(255, 255, 255));
            myGraphics.FillRectangle(brush, this.ClientRectangle);

            if (graphicsList != null)
            {
                graphicsList.Draw(myGraphics);
            }

            brush.Dispose();
        }

        private void mapControl_MouseMoveEvent(object sender, AxMapXLib.CMapXEvents_MouseMoveEvent e)
        {
            if (e.button == 1)
                tools[(int)activeTool].OnMouseMove(this, e);
            else
                this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// Set dirty flag (file is changed after last save operation)
        /// </summary>
        public void SetDirty()
        {
            //DocManager.Dirty = true;
        }

        private void mapControl_MouseUpEvent(object sender, AxMapXLib.CMapXEvents_MouseUpEvent e)
        {
            if (e.button == 1)
                tools[(int)activeTool].OnMouseUp(this, e);
        }

    }
}
